<?php
/**
 * Created by Vscode.
 * User: xiayu
 * Date: 2022/11/24
 * Time: 10:27
 */

namespace app\service\wechat;
// use EasyWeChat\Factory;
use app\service\WechatService;
use think\facade\Cache;
use app\model\SubscribeRecord;
/**
 * @mixin \EasyWeChat\OfficialAccount\Application
 * Class WechatService
 * @package app\service
 */
class WechatH5Service
{

    /**
     * @var WechatService
     */
    protected $wechat;

    const ACCESS_TOKEN_CACHE_KEY = 'wwuye_wechat_h5_token';


    /**
     * @Description: 获取AccessToken
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 11:24:07
     * @version: 1.0.0
     * @return [type]
     */    
    public function getAccessToken(){

        if(Cache::get(self::ACCESS_TOKEN_CACHE_KEY)){

            return Cache::get(self::ACCESS_TOKEN_CACHE_KEY);

        }else{
            $this->wechat = $wechat = new WechatService();
            $accessToken = $wechat->access_token;
            $token = $accessToken->getToken();
            Cache::set(self::ACCESS_TOKEN_CACHE_KEY, $token['access_token'], $token['expires_in']-60);
            return $token['access_token'];
            // return '已失效';
        }
    }

    /**
     * @Description: 获取公众号用户信息
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 13:50:51
     * @version: 1.0.0
     * @return [type]
     */    
    public  function getUserInfo($openId){

        $this->wechat = $wechat = new WechatService();

        $user = $wechat->user->get($openId);
        
        return $user;
    }

    /**
     * @Description: 获取临时二维码
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 16:31:19
     * @version: 1.0.0
     * @return [type]
     */    
    public function getQRcode(){
        $this->wechat = $wechat = new WechatService();
        $result = $wechat->qrcode->temporary('foo', 6 * 24 * 3600);
        $url = $wechat->qrcode->url($result['ticket']);
        return $url;
    }

    /**
     * @Description: 发送催缴信息
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 17:10:14
     * @version: 1.0.0
     * @return [type]
     */    
    public function sendWorthMessage($data = null){
        $this->wechat = $wechat = new WechatService();
        $res =  $wechat->template_message->send([
                'touser' => $data['touser'],
                'template_id' => '9JCFFMc8c-XzuvnAQD6uNnxeoAiPU2Rf6sxaqKNtFds',
                'url' => 'http://apartment.liuchaoai.com/client/#/',
                'data' => [
                    "first" => [
                        "value" => '您有房租待缴账单，请您及时查询账单并缴费，谢谢您的合作。'
                    ],
                    "keyword1" => [
                        "value" => $data['reale_name']
                    ],
                    "keyword2" => [
                        "value" => $data['fee'].'元'
                    ],
                    "remark" => [
                        "value" => '点击查看详情,感谢您的使用！'
                    ]
                ],
            ]);
        SubscribeRecord::addRecord($res, $data, 'sendWorthMessage');
        return $res;
    }

    /**
     * @Description: 发送审核模板消息通知
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 17:26:04
     * @version: 1.0.0
     * @return [type]
     */    
    public function sendAuditMessage($data = null){
        $this->wechat = $wechat = new WechatService();
        $res =  $wechat->template_message->send([
            'touser' => $data['touser'],
            'template_id' => 's8c1KPFMl3WLy5se42bE7Ay1JRbSzzq3417TZ5WB0a4',
            'url' => 'http://apartment.liuchaoai.com/client/#/',
            'data' => [
                "first" => [
                    "value" => '您的申请已通过审核！'
                ],
                "keyword1" => [
                    "value" => $data['real_name']
                ],
                "keyword2" => [
                    "value" => $data['room_name']
                ],
                "keyword3" => [
                    "value" => $data['time']
                ],
                "remark" => [
                    "value" => '点击查看详情,感谢您的使用！'
                ]
            ],
        ]);
        SubscribeRecord::addRecord($res, $data, 'sendAuditMessage');
        return $res;
    }

    /**
     * @Description: 发送通知维修员模板消息通知
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 17:26:04
     * @version: 1.0.0
     * @return [type]
     */
    public function sendRepairMessage($data = null)
    {
        $this->wechat = $wechat = new WechatService();
        $res =  $wechat->template_message->send([
            'touser' => $data['touser'],
            'template_id' => 'D_LU7RuR2n6vQNQWBPSu1TYPTdhE_Kkd1hsOqnVOZys',
            'url' => 'http://apartment.liuchaoai.com/client/?type_id=6#/pages/Mstaff/Mstaff',
            'data' => [
                "first" => [
                    "value" => '报修单待维修通知'
                ],
                "keyword1" => [
                    "value" => $data['time'],
                ],
                "keyword2" => [
                    "value" => $data['content'],
                ],
                "remark" => [
                    "value" => '点击查看详情,感谢您的使用！'
                ]
            ],
        ]);
        // halt($res['errcode']);
        SubscribeRecord::addRecord($res,$data, 'sendRepairMessage');

        return $res;
    }

    /**
     * @Description: 发送通知申请驳回模板消息通知
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 17:26:04
     * @version: 1.0.0
     * @return [type]
     */
    public function sendDownMessage($data = null)
    {
        // halt($data);
        $this->wechat = $wechat = new WechatService();
        $res =  $wechat->template_message->send([
            'touser' => $data['touser'],
            'template_id' => 'dNtyr5eq1ZrhpgejPTKQx-tRe8VxQ5M3FlElQfXAa9c',
            'url' => 'http://apartment.liuchaoai.com/client/#/',
            'data' => [
                "first" => [
                    "value" => $data['back_mark']
                ],
                "keyword1" => [
                    "value" => $data['real_name'],
                ],
                "keyword2" => [
                    "value" => $data['status'],
                ],
                "keyword3" => [
                    "value" => $data['time'],
                ],
                "keyword4" => [
                    "value" => $data['admin'],
                ],
                "remark" => [
                    "value" => '点击查看详情,感谢您的使用！'
                ]
            ],
        ]);
        // halt($res['errcode']);
        SubscribeRecord::addRecord($res, $data, 'sendDownMessage');

        return $res;
    }
    /**
     * @Description: 发送退宿申请模板消息通知
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 17:26:04
     * @version: 1.0.0
     * @return [type]
     */
    public function sendOutMessage($data = null)
    {
        // halt($data);
        $this->wechat = $wechat = new WechatService();
        $res =  $wechat->template_message->send([
            'touser' => $data['touser'],
            'template_id' => 'dPwOnCwynEdVRUtDaQ8853LBEp2zPFKMtuR3kp8FFyE',
            'url' => 'http://apartment.liuchaoai.com/client/#/',
            'data' => [
                "first" => [
                    "value" => '您的退宿申请已通过'
                ],
                "keyword1" => [
                    "value" => $data['user_id'],
                ],
                "keyword2" => [
                    "value" => $data['real_name'],
                ],
                "keyword3" => [
                    "value" => $data['status'],
                ],
                "keyword4" => [
                    "value" => $data['time'],
                ],
                "remark" => [
                    "value" => '点击查看详情,感谢您的使用！'
                ]
            ],
        ]);
        // halt($res['errcode']);
        SubscribeRecord::addRecord($res, $data, 'sendOutMessage');

        return $res;
    }
}